from osv import fields, osv
from tools.translate import _

class product_product(osv.osv):
    
    _description = "Add Budget Link to Product"
    _inherit = "product.product"
    _name = "product.product"
    _inherit = "product.product"
    
    _columns={
              'kdvn_budget_id':fields.many2one("account.budget.post","Budget")
              }
product_product()


class account_budget_post(osv.osv):
    _name = 'account.budget.post'
    _description = 'Budgetary Position'
    _inherit='account.budget.post'
    _columns={
              'set_default':fields.boolean('Set default value',help='If set default this budget automatically fill-in 0 when project created')
              }
    _defaults={
               'set_default':lambda *a:False
               }
    
    def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=80):
        if not args:
            args=[]
        if not context:
            context={}
        ids = []
        if name:
            ids = self.search(cr, user, [('code','=',name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('code',operator,name)]+ args, limit=limit,context=context)
        if not ids:
            ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit,context=context)
        return self.name_get(cr, user, ids, context=context)
#    def init(self,cr):
    #Trigger for insert automatically budget
#        cr.execute("Select tgname from pg_trigger where tgname='trginsertbudget_data'");
#        res=cr.fetchall()
#        if res:
#            cr.execute("drop TRIGGER trginsertbudget_data on kdvn_project")
#        cr.execute("CREATE OR REPLACE FUNCTION trginsertbudget_data()\
#              RETURNS trigger AS\
#                $BODY$ begin\
#                    begin\
#                        insert INTO crossovered_budget_lines \
#                (analytic_account_id,general_budget_id,planned_amount,crossovered_budget_id,create_date) \
#                (Select new.id as analytic_account_id,id as general_budget_id,0 as planned_amount,1 as crossovered_budget_id,now() as create_date  from account_budget_post where set_default=True);\
#                insert INTO crossovered_budget_m_lines \
#                (analytic_account_id,general_budget_id,planned_amount,crossovered_budget_id,create_date)\
#                (Select new.id as analytic_account_id,id as general_budget_id,0 as planned_amount,1 as \
#                crossovered_budget_id,now() as create_date  from account_budget_post where set_default=True);\
#                end; \
#                end if;\
#                Return NULL;\
#                end;$BODY$\
#                  LANGUAGE 'plpgsql' VOLATILE\
#                  COST 100;")
#        
#        cr.execute("CREATE TRIGGER trginsertbudget_lines\
#                      AFTER INSERT\
#                      ON account_analytic_account\
#                      FOR EACH ROW\
#                      EXECUTE PROCEDURE trgfninsertbudget_lines();")
account_budget_post()
